<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

</body>
<script>
var username = 'xujian';
// 建立连接
<!--服务端使用SseEmitter时使用-->
var source = new EventSource('http://localhost:8080/sseEmitter/connect/' + username);
<!--服务端不使用SseEmitter时使用-->
<!--var source = new EventSource('http://localhost:8080/sseEmitter/data');-->

document.write(username + '正在连接...<br>');

source.addEventListener('open', function (e) {
    document.write(username + '连接成功～<br>');
}, false);

source.addEventListener('error', function (e) {
    document.write(username + '连接建立错误！<br>');
});

source.addEventListener('psh', function (e) {
    document.write('收到消息：' + e.data + '<br>');
});

source.addEventListener('message', function (e) {
    document.write('收到消息：' + e.data + '<br>');
});
</script>
</html>